<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('settings.inc');
require_once('crypt.inc');
require_once('helper.inc');

if (!isset($menu)) {
   $menu = $_REQUEST['menu'];
}

if (!isset($func)) {
   $func = $_REQUEST['func'];
}

if (!isset($unum)) {
   $unum = $_COOKIE['TPMunum'];
}

if (!isset($rstufe)) {
   $rstufe = $_COOKIE['TPMrstufe'];
}

if (!isset($unum) || !isset($rstufe)) {
   Output(-1, 141, "<p>Dieses System arbeitet mit
      <a href=\"http://www.www-kurs.de/cookies.htm\" target=\"_blank\">Cookies</a>.
      Leider hat ihr Browser mein <i>Cookie</i> nicht akzeptiert.
      Bitte aktivieren Sie die Annahme von Cookies in Ihrem Browser,
      da sonst ein Arbeiten hier nicht m&ouml;glich ist.<br><br>
      Alle hier gesetzten Cookies werden von Ihrem Browser <b>automatisch</b>
      nach dem Abmelden oder sp&auml;testens nach 24 Stunden gel&ouml;scht
      Was immer fr&uuml;her eintritt.</p>\n");
   require('footer.inc');
   exit;
}

$un = decrypt($unum);
$rs = decrypt($rstufe);

if (((isset($_REQUEST['plan']) || $menu == 2) && $rs >= 3)) { //|| ((isset($_REQUEST['verwalt']) || $menu == 4) && $rs >= 2)) {
   $db = OpenDB();
   $query = "select mi_hacker from mitarbeiter where mi_num = $un";
   $result = QueryDB($db, $query);
   $data = fetchDB($result, 0);
   $mi_hacker = $data[0] + 1;
   $query = "update mitarbeiter set mi_hacker = $mi_hacker where mi_num = $un";
   TqueryDB($db, $query);
   closeDB($db);
   unset($db);
   unset($query);
   unset($result);
   unset($data);

   if ($mi_hacker > 3) {
      $s = "Sie haben bereits mehrfach versucht dieses Programm\n";
      $s .= "illegal zu manipulieren!<br>\n";
      $s .= "Der Administrator wurde per E-Mail dar&uuml;ber informiert\n";
      $s .= "und ihr Zugang <b>gesperrt</b>! Um wieder freigeschalten\n";
      $s .= "zu werden, wenden sie sich bitte an ihren Administrator!";
      echo "<p>" . GetMessage(-1, 90, $s) . "</p>";
      unset($mi_hacker);
      require('footer.inc');
      exit;
   } else {
      Output(-1, 142, "<p><b>ACHTUNG!</b><br>Sie haben (mehrfach) versucht dieses
         System zu manipulieren! Der Systemadministrator wurde dar&uuml;ber
         bereits per E-Mail informiert. Falls sie es noch einmal versuchen,
         wird Ihr <b>Zugang gesperrt</b>!</p>");
      unset($mi_hacker);
      require('footer.inc');
      exit;
   }

}

unset($un);
unset($rs);
unset($unum);
unset($rstufe);

# Hauptmenue
if (isset($_REQUEST['ist'])) { $knopf = 1; $headline = 10; $menu = 1; }
if (isset($_REQUEST['plan'])) { $knopf = 2; $headline = 9; $menu = 2; }
if (isset($_REQUEST['auswert'])) { $knopf = 3; $headline = 8; $menu = 3; }
if (isset($_REQUEST['verwalt'])) { $knopf = 4; $headline = 4; $menu = 4; }

# Planungsmenue
if (isset($_REQUEST['plneu'])) { $knopf = 10; $headline = 19; }
if (isset($_REQUEST['plchange'])) { $knopf = 11; }
if (isset($_REQUEST['plumplan'])) { $knopf = 12; }

# Auswertungsmenue
if (isset($_REQUEST['gantt'])) { $knopf = 20; }
if (isset($_REQUEST['pjplan'])) { $knopf = 21; $headline = 27; }
if (isset($_REQUEST['stamm'])) { $knopf = 22; }
if (isset($_REQUEST['IST'])) { $knopf = 23; $headline = 25; }
if (isset($_REQUEST['statusb'])) { $knopf = 24; $headline = 26; }
if (isset($_REQUEST['planung'])) { $knopf = 25; $headline = 29; }
if (isset($_REQUEST['ressource'])) { $knopf = 26; $headline = 30; }
if (isset($_REQUEST['auswertung'])) { $knopf = 27; $headline = 31; }
if (isset($_Request['revisor'])) { $knopf = 28; $headline = 34; }

# Verwaltungsmenue
if (isset($_REQUEST['mi'])) { $knopf = 30; $headline = 5; }
if (isset($_REQUEST['pj'])) { $knopf = 31; $headline = 11; }
if (isset($_REQUEST['tm'])) { $knopf = 32; $headline = 14; }
if (isset($_REQUEST['sgf'])) { $knopf = 33; $headline = 15; }
if (isset($_REQUEST['abt'])) { $knopf = 34; $headline = 16; }
if (isset($_REQUEST['land'])) { $knopf = 35; $headline = 17; }
if (isset($_REQUEST['role'])) { $knopf = 36; $headline = 18; }
if (isset($_REQUEST['status'])) { $knopf = 37; $headline = 22; }
if (isset($_REQUEST['phase'])) { $knopf = 38; $headline = 24; }
if (isset($_REQUEST['settings'])) { $knopf = 39; $headline = 23; }
if (isset($_REQUEST['templates'])) { $knopf = 40; $headline = 28; }
if (isset($_REQUEST['pw'])) { $knopf = 41; $headline = 32; }
if (isset($_REQUEST['journal'])) { $knopf = 42; $headline = 33; }
if (isset($_REQUEST['kunde'])) { $knopf = 43; $headline = 34; }
if (isset($_REQUEST['anteil'])) { $knopf = 44; $headline = 35; }
if (isset($_REQUEST['anrede'])) { $knopf = 45; $headline = 36; }
if (isset($_REQUEST['category'])) { $knopf = 46; $headline = 37; }
if (isset($_REQUEST['costloc'])) { $knopf = 47; $headline = 38; }
if (isset($_REQUEST['kgroup'])) { $knopf = 48; $headline = 39; }
if (isset($_REQUEST['temptagsatz'])) { $knopf = 49; $headline = 40; }
if (isset($_REQUEST['pjclose'])) { $knopf = 50; $headline = 41; }

require_once('menu.inc');

function EditProject($headline, $rstufe, $menu, $unum) {
	# Zunaechst pruefen wir, ob der Anwender ueberhaupt dazu berechtigt
	# ist.
	if ($rstufe > 1) {
	   Error(GetMessage(-1, 50, "Berechtigungsfehler!"));
	   return;
	}

	$nav = "menu=$menu&headline=11";
	echo "<form name=\"edit_pj\" action=\"edit_pj.php\" method=\"post\">\n";
	echo "<input type=\"hidden\" name=\"headline\" value=\"11\">\n";
	echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
	echo "<input type=\"hidden\" name=\"pjnew\" value=\"pjnew\">\n";

	echo "<table class=\"indent\"><tr><td>";
	Button(GetMessage(-1, 143, "Neues Projekt"), "edit_pj");
	echo "</td></tr></table>\n";

	$db = OpenDB();
	$query = "select count(*) from project";

	if (!($result = QueryDB($db, $query))) {
	   CloseDB($db);
	   return;
	}

	$data = fetchDB($result, 0);

	if ($data[0] > 0) {
	   echo "<table class=\"sel\">\n";
	   echo "<tr><th class=\"sel\">" . GetMessage($db, 144, "Aktion") . "</th>";
	   echo "<th class=\"sel\">" . GetMessage($db, 145, "Nummer") . "</th>";
	   echo "<th class=\"sel\">" . GetMessage($db, 146, "Bezeichnung") . "</th>";
	   echo "<th class=\"sel\">" . GetMessage($db, 147, "Projektleiter") . "</th>";
	   echo "<th class=\"sel\">" . GetMessage($db, 148, "Status/Art") . "</th></tr>\n";

	   $query = "select pr_num, pr_name, pr_pl, pr_status from project ";
	   $query .= "order by pr_num";

	   if (!($result = QueryDB($db, $query))) {
	      closeDB($db);
	      return;
	   }

	   $numrows = numrowsDB($result);
	   $row = 0;

	   while ($row < $numrows) {
	      $data = fetchDB($result, $row);
	      $pr_num = $data[0];
	      $pr_name = $data[1];
	      $pr_pl = $data[2];
	      $pr_status = $data[3];

	      if ($pr_status == 0) {
	         $query = "select pl_status from plan where ";
	         $query .= "pl_prnum = $pr_num order by pl_lfd desc";

		 if (!($respl = QueryDB($db, $query))) {
		    closeDB($db);
		    return;
		 }

		 $menge = numrowsDB($respl);

		 if ($menge > 0) {
		    $data = fetchDB($respl, 0);
		    $pl_status = $data[0];

		    if ($menge > 1 && $pl_status == 1) {
		       $data = fetchDB($respl, 1);
		       $pl_status = $data[0];
		    }
		 } else
		    $pl_status = 7;	// Hilfsnummer fuer Projekte ohne Plan
	      } else
	         $pl_status = 0;

	      switch ($pl_status) {
	         case 1: $ps = GetMessage($db, 149, "In Erstellung / Projekt"); break;
		 case 2: $ps = GetMessage($db, 150, "Freigegeben / Projekt"); break;
		 case 4: $ps = GetMessage($db, 151, "Aktiv / Projekt"); break;
		 case 5: $ps = GetMessage($db, 152, "Inaktiv / Projekt"); break;
		 case 6: $ps = GetMessage($db, 153, "Abgeschlossen / Projekt"); break;
		 case 7: $ps = GetMessage($db, 154, "&minus; / Projekt"); break;
		 default: $ps = GetMessage($db, 155, "&minus; / Konto");
	      }

	      $query = "select mi_vname, mi_nname from mitarbeiter where mi_num = $pr_pl";

	      if (!($result2 = QueryDB($db, $query))) {
	         closeDB($db);
		 return;
	      }

	      $data = fetchDB($result2, 0);
	      $vname = $data[0];
	      $nname = $data[1];
	      echo "<tr><td class=\"selakt\"><a href=\"#\" onClick=\"javascript:MoveTo('edit_pj.php','pjedit=$pr_num&$nav')\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></a>";
	      echo "<a href=\"#\" onClick=\"javascript:MoveTo('edit_pj.php','pjdel=$pr_num&$nav')\"><img src=\"image/editdelete.png\" border=0 alt=\"Projekt loeschen\"></a></td>";
	      echo "<td class=\"sel\">$pr_num</td><td class=\"sel\">$pr_name</td><td class=\"sel\">$nname $vname</td>\n";
	      echo "<td class=\"sel\">$ps</td></tr>\n";
	      $row++;
	   }

	   echo "</table>\n";

	   echo "<table class=\"indent\"><tr><td>";
	   Button(GetMessage($db, 143, "Neues Projekt"), "edit_pj");
	   echo "</td></tr></table>\n";
	}

	echo "</form>\n";
	closeDB($db);
}

function EditKey($key, $headline, $rstufe, $menu, $unum) {
	$nav = "menu=$menu&headline=$headline";
	# Zunaechst pruefen wir, ob der Anwender ueberhaupt dazu berechtigt
	# ist.
	if ($rstufe > 1 && $rstufe != 6) {
	   Error(GetMessage(-1, 50, "Berechtigungsfehler!"));
	   return;
	}

?>
<form name="<? echo"$key"; ?>" action="<? echo "$key"; ?>.php" method="post">
<input type="hidden" name="headline" value="<? echo "$headline"; ?>">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="new<? echo "$key"; ?>" value="new">
<table class="indent"><tr><td>
<?php
	Button(GetMessage(-1, 156, "Hinzuf&uuml;gen"), $key);
	echo "</td></tr></table>\n";
	$db = OpenDB();
	$query = "select * from key_$key";
	$result = QueryDB($db, $query);

	if (!$result) {
	   Error("SQL-Error: $query");
	   require('footer.inc');
	   return;
	}

	$numrows = numrowsDB($result);

	if ($numrows > 0) {
?>
<table class="sel">
   <tr>
      <th class="sel"><? Output($db, 157, "Aktion"); ?></th>
      <th class="sel"><? Output($db, 158, "Key"); ?></th>
      <th class="sel"><? Output($db, 159, "Text"); ?></th>
   </tr>
<?php
	   $row = 0;

	   while ($row < $numrows) {
	      $data = fetchDB($result, $row);
	      $num = $data[0];
	      $txt = $data[1];
	      echo "<tr><td class=\"selakt\"><a href=\"#\" onClick=\"javascript:MoveTo('$key.php','edit$key=$num&$nav')\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></a></td>";
	      echo "<td class=\"sel\">$num</td><td class=\"sel\">$txt</td></tr>";
	      $row++;
	   }

	   echo "</table>\n<table class=\"indent\"><tr><td>";
	   Button(GetMessage($db, 156, "Hinzuf&uuml;gen"), $key);
	   echo "</td></tr></table>\n";
//           echo "<button type=\"submit\" name=\"new$key\" value=\"new\"><img src=\"image/button_new$key.png\" border=0 alt=\"Neu\"></button>\n";
	}

	echo "</form>\n";
	closeDB($db);
}

function EditTemplates() {
	global $menu;

	# Zunaechst pruefen wir, ob der Anwender ueberhaupt dazu berechtigt
	# ist.
	if ($rstufe > 1) {
	   Error(GetMessage(-1, 50, "Berechtigungsfehler!"));
	   return;
	}

	$nav = "menu=$menu&headline=28";
	echo "<form name=\"templates\" action=\"templates.php\" method=\"post\">\n";
	echo "<input type=\"hidden\" name=\"headline\" value=\"28\">\n";
	echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
	echo "<input type=\"hidden\" name=\"func\" value=\"tempnew\">\n";
	echo "<input type=\"hidden\" name=\"tempnew\" value=\"tempnew\">\n";
	echo "<table class=\"indent\"><tr><td>\n";
	Button("Neues Template", "templates");
	echo "</td></tr></table>\n";

	$db = OpenDB();
	$query = "select te_num, te_name, te_zweck from tempidx order by te_name";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$row = 0;

	echo "<table class=\"sel\" width=\"80%\">\n";
	echo "<tr><th class=\"sel\">" . GetMessage($db, 160, "Akt.") . "</th>\n";
	echo "<th class=\"sel\">" . GetMessage($db, 161, "Name") . "</th>\n";
	echo "<th class=\"sel\">" . GetMessage($db, 162, "Zweck") . "</th></tr>\n";

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $te_num = $data[0];
	   $te_name = $data[1];
	   $te_zweck = $data[2];
	   echo "<tr><td class=\"selakt\" valign=\"top\"><table border=0 cellspacing=0 cellpadding=0><tr><td>";
	   echo "<a href=\"#\" onClick=\"javascript:MoveTo('templates.php','tempedit=$te_num&$nav')\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></a>";
	   echo "</td><td>";
	   echo "<a href=\"#\" onClick=\"javascript:MoveTo('templates.php','tempdel=$te_num&$nav')\"><img src=\"image/editdelete.png\" border=0 alt=\"Loeschen\"></a>";
	   echo "</td></tr></table></td>\n";
	   echo "<td class=\"selakt\" valign=\"top\"><a href=\"#\" onClick=\"javascript:MoveTo('templates.php','edittemp=$te_num&$nav')\">$te_name</a></td>\n";
	   echo "<td class=\"sel\">$te_zweck</td></tr>\n";
	   $row++;
	}

	echo "</table>\n";
	echo "<table class=\"indent\"><tr><td>\n";
	Button(GetMessage($db, 163, "Neues Template"), "templates");
	echo "</td></tr></table>\n";
	echo "</form>\n";
	closeDB($db);
}

# Folgende Funktion zeigt eine Maske zum setzen / aendern der
# Grundeinstellungen des Programms an. Die eingegebenen Daten
# werden in einer Datei gespeichert!
#
function Settings() {
	global $rstufe;
	global $menu;

	if ($rstufe > 1) {
	   Error(GetMessage(-1, 50, "Berechtigungsfehler!"));
	   return;
	}

	$ini_array = parse_ini_file("setup/settings.dat");
	$periode = $ini_array['periode'];
	$verteil = $ini_array['verteil'];
	$einheit = $ini_array['einheit'];
	$leneinheit = $ini_array['leneinheit'];
	$phase = $ini_array['phase'];
	$numphase = $ini_array['numphase'];
	$dtformatshort = $ini_array['dtformatshort'];
	$pmlight = $ini_array['pmlight'];
	$dtformatlong = $ini_array['dtformatlong'];
	$datetime = $ini_array['datetime'];
	$role = $ini_array['role'];
	$ist_erledigung = $ini_array['ist_erledigung'];
	$title_str = $ini_array['title'];
	$menucolor = $ini_array['menucolor'];
	$ist_future = $ini_array['ist_future'];
	$pj_lock = $ini_array['pj_lock'];
	$statusbericht = $ini_array['statusbericht'];
	$shortcuts = $ini_array['shortcuts'];
	$verrmodul = $ini_array['verrmodul'];
	$fixkunde = $ini_array['fixkunde'];
	$stdwhg = $ini_array['stdwhg'];
	$language = $ini_array['language'];
	$pjcopy = $ini_array['pjcopy'];

	if (!isset($shortcuts))
	   $shortcuts = true;

	if (!isset($verrmodul))
	   $verrmodul = false;

	if (!isset($stdwhg) || $stdwhg <= 0)
	   $stdwhg = 42;		// EUR; Euro

	if (!isset($pjcopy))
	   $pjcopy = 3;
?>
<form name="settings" action="forms.php" method="post">
   <input type="hidden" name="func" value="SaveSettings">
   <input type="hidden" name="menu" value="<? echo "$menu"; ?>">
   <input type="hidden" name="dtformatlong" value="<? echo "$dtformatlong"; ?>">
   <input type="hidden" name="datetime" value="<? echo "$datetime"; ?>">
   <input type="hidden" name="statusbericht" value="<? echo "$statusbericht"; ?>">
   <input type="hidden" name="verrmodul" value="<? echo "$verrmodul"; ?>">
   <input type="hidden" name="headline" value="23">
<table class="input" border=0>
   <tr>
      <th colspan=2 class="sel"><? Output(-1, 91, "Einstellungen f&uuml;r die Planung"); ?></th>
   </tr>
   <tr>
      <td><? Output(-1, 92, "L&auml;nge einer Periode:"); ?></td>
      <td><select name="periode" disabled>
             <option value="1" <? if ($periode == 1) echo "selected"; echo ">"; Output(-1, 93, "Jahr"); ?></option>
	     <option value="2" <? if ($periode == 2) echo "selected"; echo ">";  Output(-1, 94, "Quartal");  ?></option>
	     <option value="3" <? if ($periode == 3) echo "selected"; echo ">";  Output(-1, 95, "Monat");  ?></option>
	     <option value="4" <? if ($periode == 4) echo "selected"; echo ">";  Output(-1, 96, "Woche");  ?></option>
	     <option value="5" <? if ($periode == 5) echo "selected"; echo ">";  Output(-1, 97, "Tag");  ?></option>
	  </select>
      </td>
   </tr>
   <tr>
      <td><? Output(-1, 98, "Verteilung der Auw&auml;nde:"); ?></td>
      <td><select name="verteil" disabled>
             <option value="1" <? if ($verteil == 1) echo "selected"; ?>><? Output(-1, 99, "Normalverteilung"); ?></option>
	     <option value="2" <? if ($verteil == 2) echo "selected"; ?>><? Output(-1, 100, "Endlastig"); ?></option>
	     <option value="3" <? if ($verteil == 3) echo "selected"; ?>><? Output(-1, 101, "Anfangslastig"); ?></option>
	     <option value="4" <? if ($verteil == 4) echo "selected"; ?>><? Output(-1, 102, "Anfang&minus; und Endlasig"); ?></option>
	     <option value="5" <? if ($verteil == 5) echo "selected"; ?>><? Output(-1, 103, "Mittellastig"); ?></option>
	     <option value="6" <? if ($verteil == 6) echo "selected"; ?>><? Output(-1, 104, "Individuell"); ?></option>
	  </select>
      </td>
   </tr>
   <tr>
      <td><? Output(-1, 105, "Gr&ouml;&szlig;e einer Arbeitseiheit:"); ?></td>
      <td><select name="einheit" disabled>
             <option value="1" <? if ($einheit == 1) echo "selected"; ?>><? Output(-1, 106, "Stunde"); ?></option>
	     <option value="2" <? if ($einheit == 2) echo "selected"; ?>><? Output(-1, 107, "Tag"); ?></option>
	     <option value="3" <? if ($einheit == 3) echo "selected"; ?>><? Output(-1, 108, "Woche"); ?></option>
	     <option value="4" <? if ($einheit == 4) echo "selected"; ?>><? Output(-1, 109, "Monat"); ?></option>
	     <option value="5" <? if ($einheit == 5) echo "selected"; ?>><? Output(-1, 110, "Quartal"); ?></option>
	     <option value="6" <? if ($einheit == 6) echo "selected"; ?>><? Output(-1, 111, "Jahr"); ?></option>
	  </select>
      </td>
   </tr>
   <tr>
      <td><? Output(-1, 112, "L&auml;nge einer Arbeitseinh. in Stunden:"); ?></td>
      <td><input type="text" class="inputmust" name="leneinheit" value="<? echo "$leneinheit"; ?>" size=5 maxlength=5>
   </tr>
   <tr>
      <td><? Output(-1, 113, "Projektphasen verwenden?"); ?></td>
      <td><input type="checkbox" name="phase" value="1" <? if (isset($phase) && $phase) echo "checked"; ?>>
   </tr>
   <tr>
      <td><? Output(-1, 114, "Tasks nach Phasen nummerieren?"); ?></td>
      <td><input type="checkbox" name="numphase" value="1" <? if (isset($numphase) && $numphase) echo "checked"; ?>>
   </tr>
   <tr>
      <td><? Output(-1, 115, "Datumsformat:"); ?></td>
      <td><select name="dtformatshort">
             <option value="d.m.Y" <? if ($dtformatshort == "d.m.Y") echo "selected"; ?>>TT.MM.JJJJ</option>
	     <option value="d.m.y" <? if ($dtformatshort == "d.m.y") echo "selected"; ?>>TT.MM.JJ</option>
	     <option value="j.n.Y" <? if ($dtformatshort == "j.n.Y") echo "selected"; ?>>T.M.JJJJ</option>
	     <option value="j.n.y" <? if ($dtformatshort == "j.n.y") echo "selected"; ?>>T.M.JJ</option>
	     <option value="m/d/Y" <? if ($dtformatshort == "m/d/Y") echo "selected"; ?>>MM/DD/YYYY</option>
	     <option value="m/d/y" <? if ($dtformatshort == "m/d/y") echo "selected"; ?>>MM/DD/YY</option>
	     <option value="Y-m-d" <? if ($dtformatshort == "Y-m-d") echo "selected"; ?>>YYYY-MM-DD</option>
	  </select>
      </td>
   </tr>
   <tr>
      <td><? Output(-1, 116, "Periodensicht:"); ?></td>
      <td><input type="checkbox" name="pmlight" value="1" <? if (isset($pmlight) && $pmlight) echo "checked"; ?>></td>
   </tr>
   <tr>
      <td><? Output(-1, 117, "Mitarbeiterrolle in Plan:"); ?></td>
      <td><input type="checkbox" name="role" value="1" <? if (isset($role) && $role) echo "checked"; ?>></td>
   </tr>
   <tr>
      <td><? Output(-1, 118, "Prozent erledigt bei Zeiterfassung:"); ?></td>
      <td><input type="checkbox" name="ist_erledigung" value="1" <? if (isset($ist_erledigung) && $ist_erledigung) echo "checked"; ?>></td>
   </tr>
   <tr>
      <td><? Output(-1, 119, "Taskgenaue Zeiterfassung:"); ?></td>
      <td><input type="checkbox" name="ist_future" value="1" <? if (isset($ist_future) && $ist_future) echo "checked"; ?>></td>
   </tr>
   <tr>
      <td><? Output(-1, 120, "Projekt Teilabschluss durch:"); ?></td>
      <td><select name="pj_lock">
             <option value="0"><? Output(-1, 121, "Projektleiter"); ?></option>
	     <option value="1" <? if (isset($pj_lock) && $pj_lock == 1) echo "selected"; ?>><? Output(-1, 122, "Kontroller"); ?></option>
	  </select>
      </td>
   </tr>
   <tr>
      <td><? Output(-1, 123, "Shortcuts:"); ?></td>
      <td><input type="checkbox" name="shortcuts" value="1" <? if (isset($shortcuts) && $shortcuts) echo "checked"; ?>></td>
   </tr>
   <tr>
      <td><? Output(-1, 398, "Plan kopieren:"); ?></td>
      <td><select name="pjcopy">
             <option value="1" <? if ($pjcopy == 1) echo "selected"; ?>><? Output(-1, 399, "Individuelles Datums f&uuml;r IST-Wert&uuml;bernahme"); ?></option>
	     <option value="2" <? if ($pjcopy == 2) echo "selected"; ?>><? Output(-1, 400, "IST-Werte bis Ultimo der letzten Periode &uuml;bernehmen"); ?></option>
	     <option value="3" <? if ($pjcopy == 3) echo "selected"; ?>><? Output(-1, 401, "IST-Werte bis letzten Teilprojektabschluss &uuml;bernehmen"); ?></option>
	  </select>
      </td>
   </tr>
<?
	if ($verrmodul) {
?>
   <tr>
      <th class="sel" colspan=2><? Output(-1, 124, "Verrechnungsmodul"); ?></th>
   </tr>
   <tr>
      <td><? Output(-1, 125, "Fixe Kundengruppen:"); ?></td>
      <td><input type="checkbox" name="fixkunde" value="1" <? if (isset($fixkunde) && $fixkunde) echo "checked"; ?>></td>
   </tr>
   <tr>
      <td><? Output(-1, 126, "Standardw&auml;hrung:"); ?></td>
      <td><select name="stdwhg">
<?
	   $db = OpenDB($db);
	   $query = "select wh_num, wh_whg, wh_bez from key_whg order by wh_num";

	   if (!($result = QueryDB($db, $query))) {
	      closeDB($db);
	      return;
	   }

	   $rows = numrowsDB($result);
	   $j = 0;

	   while ($j < $rows) {
	      $data = fetchDB($result, $j);
	      $wh_num = $data[0];
	      $wh_whg = $data[1];
	      $wh_bez = $data[2];

	      if ($stdwhg == $wh_num)
		 echo "<option value=\"$wh_num\" selected>$wh_whg $wh_bez</option>\n";
	      else
		 echo "<option value=\"$wh_num\">$wh_whg $wh_bez</option>\n";

	      $j++;
	   }

	   closeDB($db);
?>
          </select>
      </td>
   </tr>
<?
	}
?>
   <tr>
      <th class="sel" colspan=2><? Output(-1, 127, "Einstellungen der Ansicht"); ?></th>
   </tr>
   <tr>
      <td><? Output(-1, 215, "Standardsprache:"); ?></td>
      <td><select name="language">
             <option value="ger">Deutsch</option>
             <option value="eng"<? if ($language == "eng") echo " selected"; ?>>English</option>
	  </select>
      </td>
   </tr>
   <tr>
      <td><? Output(-1, 128, "Farbe Men&uuml;balkenunterstreichung:"); ?></td>
      <td><input type="text" name="menucolor" value="<? echo "$menucolor"; ?>" size=15 maxlength=15></td>
   </tr>
   <tr>
      <td><? Output(-1, 129, "Fenster&uuml;berschrift:"); ?></td>
      <td><input type="text" name="title_str" value="<? echo urldecode($title_str); ?>" size=30 maxlength=80></td>
   </tr>
</table>

<table class="indent"><tr><td>
<?
	Button(GetMessage(-1, 45, "Speichern"), "settings");
	echo "</td></tr></table></form>\n";
}

function SaveSettings() {
	global $rstufe;

	$periode = $_REQUEST['periode'];
	$verteil = $_REQUEST['verteil'];
	$einheit = $_REQUEST['einheit'];
	$leneinheit = $_REQUEST['leneinheit'];
	$phase = $_REQUEST['phase'];
	$numphase = $_REQUEST['numphase'];
	$dtformatshort = $_REQUEST['dtformatshort'];
	$dtformatlong = $_REQUEST['dtformatlong'];
	$datetime = $_REQUEST['datetime'];
	$pmlight = $_REQUEST['pmlight'];
	$role = $_REQUEST['role'];
	$ist_erledigung = $_REQUEST['ist_erledigung'];
	$menucolor = $_REQUEST['menucolor'];
	$title_str = urlencode($_REQUEST['title_str']);
	$ist_future = $_REQUEST['ist_future'];
	$pj_lock = $_REQUEST['pj_lock'];
	$statusbericht = $_REQUEST['statusbericht'];
	$shortcuts = $_REQUEST['shortcuts'];
	$verrmodul = $_REQUEST['verrmodul'];
	$fixkunde = $_REQUEST['fixkunde'];
	$stdwhg = $_REQUEST['stdwhg'];
	$language = $_REQUEST['language'];
	$pjcopy = $_REQUEST['pjcopy'];

	if (!isset($periode) || $periode < 1)
	   global $periode;

	if (!isset($verteil) || $verteil < 1)
	   global $verteil;

	if (!isset($einheit) || $einheit < 1)
	   global $einheit;

	if ($rstufe > 1) {
	   Error(GetMessage(-1, 50, "Berechtigungsfehler!"));
	   return false;
	}

	if (!isset($dtformatshort) || strlen($dtformatshort) < 1)
	   $dtformatshort = "j.n.Y";

	if (!isset($dtformatlong) || strlen($dtformatlong) < 1)
	   $dtformatlong = "d F Y";

	if (!isset($datetime) || strlen($datetime) < 1)
	   $datetime = "d.m.Y H:i:s";

	$err = 0;

	switch ($periode) {
	   case 5: if ($einheit > 1) $err = 1; break;
	   case 4: if ($einheit > 2) $err = 1; break;
	   case 3: if ($einheit > 3) $err = 1; break;
	   case 2: if ($einheit > 4) $err = 1; break;
	   case 1: if ($einheit > 5) $err = 1; break;
	}

	if ($err == 1) {
	   Error(GetMessage(-1, 130, "Eine Arbeitseinheit muss immer kleiner als die eingestellte Periode sein!"));
	}

	switch ($einheit) {
	   case 1: $maxlen = 1; break;
	   case 2: $maxlen = 24; break;
	   case 3: $maxlen = 24 * 7; break;
	   case 4: $maxlen = 24 * 30; break;
	   case 5: $maxlen = 24 * 91; break;
	   case 6: $maxlen = 24 * 364; break;
	}

	if ($leneinheit > $maxlen) {
	   Error(GetMessage(-1, 131, "Die von ihnen eingestellte L&auml;nge einer Arbeitseinheit &uuml;bersteigt die maximale L&auml;nge von %d Stunden!", $maxlen));
	   $err = 1;
	}

	if ($err == 1) {
	   Settings();
	   require('footer.inc');
	   return false;
	}

	if (!isset($pmlight))
	   $pmlight = 0;

	if (!isset($statusbericht))
	   $statusbericht = false;

	if (!isset($shortcuts))
	   $shortcuts = false;

	if (!isset($verrmodul))
	   $verrmodul = false;

	if (!isset($fixkunde))
	   $fixkunde = false;

	# Planungseinstellungen speichern
	$fp = fopen("setup/settings.dat", "w+");

	if (!$fp) {
	   Error(GetMessage(-1, 132, "Datei \"settings.dat\" konnte nicht angelegt werden!"));
	   return false;
	}

	$dat = "[pm]\nperiode=$periode\nverteil=$verteil\neinheit=$einheit\n";
	$dat .= "leneinheit=$leneinheit\nphase=$phase\nnumphase=$numphase\n";
	$dat .= "dtformatshort=$dtformatshort\ndtformatlong=$dtformatlong\n";
	$dat .= "datetime=$datetime\npmlight=$pmlight\nrole=$role\n";
	$dat .= "ist_erledigung=$ist_erledigung\nmenucolor=$menucolor\ntitle=$title_str\n";
	$dat .= "ist_future=$ist_future\npj_lock=$pj_lock\nstatusbericht=$statusbericht\n";
	$dat .= "shortcuts=$shortcuts\nverrmodul=$verrmodul\nfixkunde=$fixkunde\n";
	$dat .= "stdwhg=$stdwhg\nlanguage=$language\npjcopy=$pjcopy";
	fwrite($fp, $dat);
	fclose($fp);
	return true;
}

function ChangePW() {
	global $unum;
	global $rstufe;
	global $menu;

	if ($rstufe == 0 || $rstufe == 1 || $rstufe == 5) {
	   Error(GetMessage(-1, 133, "Sie sind nicht berechtigt ein Passwort zu &auml;ndern!"));
	   return;
	}

	$s = "<p><b>Hinweise f&uuml;r ein sicheres Passwort</b><br><br>\n";
	$s .= "Ein gutes Passwort sollte aus m&ouml;glichst zuf&auml;lligen\n";
	$s .= "Buchstaben und Ziffern bestehen. Zus&auml;tzlich sollten Gro&szlig;&minus;\n";
	$s .= "und Kleinbuchstaben gemischt verwendet werden.</p>\n";
	Output(-1, 134, $s);

	echo "<form name=\"forms\" action=\"forms.php\" method=\"post\">\n";
	echo "<input type=\"hidden\" name=\"headline\" value=\"32\">\n";
	echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
	echo "<input type=\"hidden\" name=\"func\" value=\"DoChangePW\">\n";

	echo "<table class=\"input\">\n";
	echo "<tr><td>" . GetMessage(-1, 135, "Altes Passwort:") . "</td>\n";
	echo "<td><input class=\"inputmust\" type=\"password\" name=\"oldpass\" size=8 maxlength=8></td></tr>\n";
	echo "<tr><td>" . GetMessage(-1, 136, "Neues Passwort:") . "</td>\n";
	echo "<td><input class=\"inputmust\" type=\"password\" name=\"newpass1\" size=8 maxlength=8></td></tr>\n";
	echo "<tr><td>" . GetMessage(-1, 137, "Passwort wiederholen:") . "</td>\n";
	echo "<td><input class=\"inputmust\" type=\"password\" name=\"newpass2\" size=8 maxlength=8></td></tr>\n";
	echo "</table></td></tr></table>\n";
	ButtonSubmit(GetMessage(-1, 138, "Passwort speichern"), "forms");
//	echo "<input type=\"submit\" name=\"submit\" value=\"Passwort speichern\">";
//	echo "<input type=\"reset\">\n";
	echo "</form>\n";
}

function DoChangePW() {
	global $unum;
	$oldpass = $_REQUEST['oldpass'];
	$newpass1 = $_REQUEST['newpass1'];
	$newpass2 = $_REQUEST['newpass2'];
	$err = 0;

	if (strlen($oldpass) == 0) {
	   Error(GetMessage(-1, 164, "Sie m&uuml;ssen das alte Passwort angeben, um es &auml;ndern zu k&ouml;nnen!"));
	   $err = 1;
	}

	$db = OpenDB();
	$query = "select mi_passwd from mitarbeiter where mi_num = $unum";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$mi_passwd = $data[0];
	$pw = md5($oldpass);

	if ($pw != $mi_passwd) {
	   Error("Das alte Passwort ist nicht korrekt!");
	   closeDB($db);
	   return;
	}

	if ($newpass1 != $newpass2) {
	   Error(GetMessage($db, 165, "Das neue Passwort muss 2 mal gleich eingegeben werden!"));
	   closeDB($db);
	   return;
	}

	if (strlen($newpass1) < 4) {
	   Error(GetMessage($db, 166, "Das neue Passwort muss mindestens 4 Zeichen lang sein!"));
	   closeDB($db);
	   return;
	}

	if ($pw == md5($newpass1)) {
	   Error(GetMessage($db, 167, "Das neue Passwort muss sich vom alten unterscheiden!"));
	   $err = 1;
	}

	if ($err > 0) {
	   closeDB($db);
	   return;
	}

	# Wegschreiben des neuen Passworts
	$pw = md5($newpass1);
	$query = "update mitarbeiter set mi_passwd = '$pw' where mi_num = $unum";

	if (!QueryDB($db, $query)) {
	   closeDB($db);
	   return;
	}

	Output($db, 168, "<p>Das Passwort wurde erfolgreich ge&auml;ndert!</p>");
	closeDB($db);
}

function TempTagsatz($edit=false) {
	global $menu;
	global $headline;
	global $rstufe;
	global $verrmodul;
	global $VisualDate;
	global $stdwhg;
	global $dtformatshort;

	if (!$verrmodul)
	   return;

	if ($rstufe != 0 && $rstufe != 1 && $rstufe != 4) {
	   Error(GetMessage(-1, 50, "Berechtigungsfehler!"));
	   return;
	}

	if (!$edit) {
	   $ini_array = parse_ini_file("setup/ration.dat");
	   $valid_from = $ini_array['valid_from'];

	   if (isset($valid_from) && strlen($valid_from) == 10) {
	      $darr = explode("-", $valid_from);
	      $dt = mktime(0, 0, 0, $darr[1], $darr[2], $darr[0]);
	      $valid_from = date($dtformatshort, $dt);
	   } else
	      $valid_from = date($dtformatshort);

	   $amount = $ini_array['amount'];
	   $nightr = $ini_array['nightr'];
	   $ration = $ini_array['ration'];
	   $kmcar = $ini_array['kmcar'];
	   $kmdrain = $ini_array['kmdrain'];
	   $kmplain = $ini_array['kmplain'];
	} else {
	   $valid_from = $_REQUEST['valid_from'];
	   $amount = $_REQUEST['amount'];
	   $nightr = $_REQUEST['nightr'];
	   $ration = $_REQUEST['ration'];
	   $kmcar = $_REQUEST['kmcar'];
	   $kmdrain = $_REQUEST['kmdrain'];
	   $kmplain = $_REQUEST['kmplain'];
	}

	$db = OpenDB();
	$query = "select wh_whg from key_whg where wh_num = $stdwhg";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$wh_whg = $data[0];
?>
<form name="tagsatz" action="forms.php" method="post">
   <input type="hidden" name="menu" value="<? echo "$menu"; ?>">
   <input type="hidden" name="headline" value="<? echo "$headline"; ?>">
   <input type="hidden" name="func" value="SaveTagsatz">
<table class="input">
   <tr>
      <th class="sel" colspan=2><? Output($db, 169, "Vorlage f&uuml;r Tagsatz"); ?></th>
   </tr>
   <tr>
      <td><? Output($db, 170, "Betr&auml;ge g&uuml;ltig ab"); ?> (<? echo "$VisualDate"; ?>):</td>
      <td><input type="text" name="valid_from" class="inputmust" value="<? echo "$valid_from"; ?>" size=10 maxlength=10 onChange="javascript:CheckDate(this, false)"></td>
   </tr>
   <tr>
      <td><? Output($db, 171, "Tagsatz:"); ?></td>
      <td><input type="text" name="amount" class="inputmust" value="<? echo FormatNum($amount, 2); ?>" size=16 maxlength=16><? echo "$wh_whg"; ?></td>
   </tr>
   <tr>
      <td><? Output($db, 172, "N&auml;chtigungspauschale:"); ?></td>
      <td><input type="text" name="nightr" value="<? echo FormatNum($nightr, 2); ?>" size=16 maxlength=16><? echo "$wh_whg"; ?></td>
   </tr>
   <tr>
      <td><? Output($db, 173, "Tagesdi&auml;ten:"); ?></td>
      <td><input type="text" name="ration" value="<? echo FormatNum($ration, 2); ?>" size=16 maxlength=16><? echo "$wh_whg"; ?></td>
   </tr>
   <tr>
      <td><? Output($db, 174, "Kilometerpauschale PKW:"); ?></td>
      <td><input type="text" name="kmcar" value="<? echo FormatNum($kmcar, 2); ?>" size=16 maxlength=16><? echo "$wh_whg"; ?></td>
   </tr>
   <tr>
      <td><? Output($db, 175, "Kilometerpauschale Zug:"); ?></td>
      <td><input type="text" name="kmdrain" value="<? echo FormatNum($kmdrain, 2); ?>" size=16 maxlength=16><? echo "$wh_whg"; ?></td>
   </tr>
   <tr>
      <td><? Output($db, 176, "Kilometerpauschale Flugzeug:"); ?></td>
      <td><input type="text" name="kmplain" value="<? echo FormatNum($kmplain, 2); ?>" size=16 maxlength=16><? echo "$wh_whg"; ?></td>
   </tr>
</table>
<table class="indent"><tr><td>
<?
	ButtonSubmit(GetMessage($db, 177, "Speichern"), "tagsatz");
	echo "</td></tr></table>\n";
	closeDB($db);
}

function SaveTagsatz() {
	global $verrmodul;

	if (!$verrmodul)
	   return;

	$valid_from = $_REQUEST['valid_from'];
	$amount = str_replace(",", ".", $_REQUEST['amount']);
	$nightr = str_replace(",", ".", $_REQUEST['nightr']);
	$ration = str_replace(",", ".", $_REQUEST['ration']);
	$kmcar = str_replace(",", ".", $_REQUEST['kmcar']);
	$kmdrain = str_replace(",", ".", $_REQUEST['kmdrain']);
	$kmplain = str_replace(",", ".", $_REQUEST['kmplain']);

	$err = 0;

	if (!isset($valid_from) || strlen($valid_from) < 6 || str_wordcount($valid_from, ".") != 3) {
	   Error(GetMessage(-1, 178, "Kein oder ung&uuml;ltiges Datum! Sie m&uuml;ssen ein g&uuml;ltiges Datum eingeben!"));
	   $err = 1;
	}

	if (!isset($amount) || strlen($amount) <= 0) {
	   Error(GetMessage(-1, 179, "Sie m&uuml;ssen einen Tagsatz gr&ouml;&szlig;er 0,0 eingeben!"));
	   $err = 1;
	}

	if ($err)
	   TempTagsatz(true);

	$fp = fopen("setup/ration.dat", "w+");

	if (!$fp) {
	   Error(GetMessage(-1, 180, "Datei \"ration.dat\" konnte nicht angelegt werden!"));
	   return;
	}

	$dat = "[ration]\nvalid_from=".DateToDBDate($valid_from)."\n";
	$dat .= "amount=$amount\nnightr=$nightr\nration=$ration\nkmcar=$kmcar\n";
	$dat .= "kmdrain=$kmdrain\nkmplain=$kmplain\n";
	fwrite($fp, $dat);
	fclose($fp);
}

if (isset($func)) {
   if ($func == "SaveSettings") {
      if (!SaveSettings())
         exit;
   }

   if ($func == "DoChangePW")
      DoChangePW();

   if ($func == "SaveTagsatz")
      SaveTagsatz();
}

switch ($knopf) {
   case 1:
      ListProj();
   break;

   case 30:
      EditMitarbeiter($headline, $rstufe, $menu, $unum);
   break;

   case 31:
      EditProject($headline, $rstufe, $menu, $unum);
   break;

   case 32:
      EditKey("tmgroup", $headline, $rstufe, $menu, $unum);
   break;

   case 33:
      EditKey("sgf", $headline, $rstufe, $menu, $unum);
   break;

   case 34:
      EditKey("abt", $headline, $rstufe, $menu, $unum);
   break;
   case 35:
      EditKey("land", $headline, $rstufe, $menu, $unum);
   break;

   case 36:
      EditKey("role", $headline, $rstufe, $menu, $unum);
   break;

   case 37:
      EditKey("status", $headline, $rstufe, $menu, $unum);
   break;

   case 38:
      EditKey("phase", $headline, $rstufe, $menu, $unum);
   break;

   case 39:
      Settings();
   break;

   case 40:
      EditTemplates();
   break;

   case 41:
      ChangePW();
   break;

   case 45:
      EditKey("anrede", $headline, $rstufe, $menu, $unum);
   break;

   case 46:
      EditKey("category", $headline, $rstufe, $menu, $unum);
   break;

   case 47:
      EditKey("costloc", $headline, $rstufe, $menu, $unum);
   break;

   case 49:
      TempTagsatz();
   break;

   case -1: break;

   default:
      if ($menu == 1) {
	 require_once('helper.inc');
	 require('start.inc');
      }
}

require('footer.inc');
?>
